# API获取的数据记录类-项目索引类
# by lw 2023-11-05

import json
import datetime


class ProjectIndex():

    def __init__(self,
                 indexId: int = 0,
                 datasourceId: int = 0,
                 tid: str = "",
                 linkurl: str = "",
                 publishTime: str = "",
                 createTime: str = None,
                 isRepeat: int = None,
                 analysisStatus: int = None):
        """构造函数

        Args:
            indexId (int, optional): 索引标识码. Defaults to 0.
            datasourceId (int, optional): 数据源标识码（来源站点标识码）. Defaults to 0.
            tid (str, optional): 任务标识码. Defaults to "".
            linkurl (str, optional): 数据页面url. Defaults to "".
            publishTime (str, optional): 数据发布时间. Defaults to "".
            createTime (str, optional): 索引创建时间. Defaults to None.
            isRepeat (int, optional): 是否重复. Defaults to None.
            analysisStatus (int, optional): 是否解析成功. Defaults to None.
        """
        self.indexId = indexId  # 索引标识码
        self.datasourceId = datasourceId  # 数据源标识码（来源站点标识码）
        self.tid = tid  # 任务标识码
        self.linkurl = linkurl  # 数据页面url
        self.publishTime = publishTime  # 数据发布时间
        if createTime == None:
            createTime = datetime.datetime.now().isoformat()
        self.createTime = createTime  # 索引创建时间
        if isRepeat == None:
            isRepeat = 0
        self.isRepeat = isRepeat  # 是否重复
        if analysisStatus == None:
            analysisStatus = 0
        self.analysisStatus = analysisStatus  # 是否解析成功

    def tostring(self) -> str:
        """将对象序列化为json字符串格式

        Returns:
            str: json字符串
        """
        return json.dumps(
            self,
            default=lambda obj: obj.__dict__,
            #   sort_keys=True,
            indent=2,
            ensure_ascii=False)

    def from_json(self, d: json) -> object:
        """对象反序列化:从json对象反序列化

        Args:
            d (json): ProjectIndex类对象对应的json对象

        Returns:
            object: ProjectIndex类对象self
        """
        self.indexId = d['indexId']
        self.datasourceId = d['datasourceId']
        self.tid = d['tid']
        self.linkurl = d['linkurl']
        self.publishTime = d['publishTime']
        self.createTime = d['createTime']
        self.isRepeat = d['isRepeat']
        self.analysisStatus = d['analysisStatus']
        return self

    def from_string(self, jstring: str) -> object:
        """ProjectIndex类对象反序列化：将描述的对象json字符串反序列化对象

        Args:
            jstring (str): 描述对象的json字符串

        Returns:
            object: ProjectIndex类对象self
        """
        return json.loads(jstring, object_hook=self.from_json)


# 测试代码：
if __name__ == '__main__':
    print("__name__:", __name__)
    # 不同的实例化：
    d = ProjectIndex()
    print("无参数实例化的对象tostring:", d.tostring())
    d = ProjectIndex(linkurl="http://hello")
    print("有参数实例化的对象tostring:", d.tostring())
    # 反序列化
    j = json.loads(d.tostring())
    print("Json Object of ProjectIndex:", j)
    d2 = ProjectIndex()
    d2.from_json(j)
    print("使用json对象实例化的对象tostring:", d2.tostring())
    d3 = ProjectIndex()
    d3.from_string(d.tostring())
    print("使用json字符串实例化的对象tostring:", d3.tostring())
